import { Loading, Message, } from 'element-ui';

function downLoadFile(url, filename, callback, timeout = 5000) {
  if (!url) {
    throw new Error('请求下载地址不能为空');
  }
  if (!filename) {
    throw new Error('文件名称不能为空');
  }
  const loading = Loading.service({
    lock: true,
    text: '请求中...',
    spinner: 'el-icon-loading',
    background: 'rgba(0, 0, 0, 0.7)',
  });
  const xhr = new XMLHttpRequest();
  xhr.open('get', url, true);
  xhr.responseType = 'blob';
  xhr.timeout = timeout;
  xhr.ontimeout = () => {
    // 超时处理
    loading.close();
    Message.error('下载超时!');
  };
  xhr.onerror = (e) => {
    Message.error('下载出错!');
    loading.close();
  };
  xhr.onload = () => {
    if (xhr.status === 200) {
      createDownLoad(xhr.response, filename, () => {
        loading.close();
        if (callback instanceof Function) {
          callback();
        }
      });
    }
  };
  xhr.send(null);
}

function createDownLoad(blob, filename, callback) {
  if (window.navigator.msSaveOrOpenBlob) {
    navigator.msSaveBlob(blob, filename);
  } else {
    var link = document.createElement('a');
    var body = document.querySelector('body');
    link.href = window.URL.createObjectURL(blob);
    link.download = filename;
    // fix Firefox
    link.style.display = 'none';
    body.appendChild(link);
    link.click();
    body.removeChild(link);
    window.URL.revokeObjectURL(link.href);
    if (callback instanceof Function) {
      callback();
    }
  }
}
export default downLoadFile;
